
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;


public class ASprite
{
    interface Constants
    {
        final static int HL_VT = 0;
        final static int HL_VC = 1;
        final static int HL_VB = 2;
        final static int HC_VT = 3;
        final static int HC_VC = 4;
        final static int HC_VB = 5;
        final static int HR_VT = 6;
        final static int HR_VC = 7;
        final static int HR_VB = 8;
    }

    private int[] palette;
    private int[][] indexed;
    private Image[] cached;

    public ASprite()
    {
        
    }
    public void loadSprite(byte[] array)
    {
        int offset = 0;
        int colors = array[offset++];
        System.out.println("D->colors: " + colors);
        //read colors from resource
        palette = new int[colors];
        //read palette from resource
        for(int i=0; i < colors; i++)
        {
            palette[i] = (array[offset++]<<16)+(array[offset++]<<8)+array[offset++];
            System.out.println("D->palette["+i+"]: " + Integer.toHexString(palette[i]));
        }
        int modules = array[offset++];
        System.out.println("D->modules: " + modules);
        //read modules from resource
        indexed = new int[modules][];
        cached = new Image[modules];
        for(int i=0; i < modules; i++)
        {
            int w = array[offset++];
            int h = array[offset++];
            indexed[i] = new int[w*h];
            System.out.println("D->w: " + w);
            System.out.println("D->h: " + h);
            for(int x=0; x<w*h; x++)
            {
                indexed[i][x] = palette[array[offset++]];
                System.out.println("D->array["+(offset-1)+"]: " + array[(offset-1)]);
                System.out.println("D->indexed["+i+"]["+x+"]: " + Integer.toHexString(indexed[i][x]));
            }
            cached[i] = Image.createRGBImage(indexed[i], w, h, false);
        }
    }

    public void paintFrame(int frame, int x, int y, int anchor)
    {

    }

    public void paintModule(Graphics g, int module, int x, int y, int anchor)
    {
        try
        {
            g.drawImage(cached[module], x, y, anchor);
        }
        catch(Exception e)
        {
            System.out.println("E-> exception on paintModule: " + e.getMessage());
        }
    }
}
